define(["angularAMD", "module/system/program/myFavourite/favouriteService"], function (angularAMD) {
    angularAMD.service('optionDialogService', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', 'favouriteService', 'RouterExtendProvider',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog ,$favouriteService, $RouterExtendProvider){
            // 20220726 modify by Alan for#107965 : 配合規格進行調整
            this.init = function(options, callback){

                let openDialog = () => {
                    $mdDialog.dialog(
                        'module/system/program/optionDialog/optionDialog.html',
                        controller
                    );
                }

                let controller = (dialog) => {
                    return {
                        css_url: 'module/system/program/optionDialog/optionDialog.css',
                        item: options.item,
                        favouriteInfo: {
                            UserNo: '',
                            FavouriteList: [],
                            DefaultProgram : ''
                        },
                        beforeShown() {
                            $favouriteService.get(config.cache.account, (data) => dialog.favouriteInfo = data);
                        },
                        executeProgram() {
                            dialog.hide();
                            $RouterExtendProvider.goToProgram(dialog.item);
                        },
                        isFavourite() {
                            if(dialog.favouriteInfo) {
                                return dialog.favouriteInfo.FavouriteList.includes(dialog.item.name);
                            } else {
                                return false;
                            }
                        },
                        addFavorite() {
                            dialog.favouriteInfo.FavouriteList.push(dialog.item.name);

                            const option = angular.copy(dialog.favouriteInfo);
                            $favouriteService.setFavouriteList(option, (data) => {
                                dialog.favouriteInfo = data;
                            });
                        },
                        removeFavorite() {
                            const index = dialog.favouriteInfo.FavouriteList.indexOf(dialog.item.name);
                            dialog.favouriteInfo.FavouriteList.splice(index, 1);

                            const option = angular.copy(dialog.favouriteInfo);
                            $favouriteService.setFavouriteList(option, (data) => {
                                dialog.favouriteInfo = data;
                            });
                        },
                        setDefault() {
                            dialog.favouriteInfo.DefaultProgram = 
                                dialog.favouriteInfo.DefaultProgram === dialog.item.name
                                    ? ''
                                    : dialog.item.name;

                            const option = angular.copy(dialog.favouriteInfo);
                            $favouriteService.setDefaultProgram(option, (data) => {
                                dialog.favouriteInfo = data;
                            });
                        },
                        back() {
                            dialog.hide();
                            callback && callback(dialog);
                        }
                    };
                }

                openDialog();
            };
        }]);
});
